package com.snatch.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.snatch.common.vo.LiveMatch;
import com.snatch.common.vo.Matchinfo;
import com.snatch.common.vo.Playinfo;
import com.snatch.common.vo.RatioDataInput;
import com.snatch.common.vo.Sportinfo;
import com.snatch.common.vo.TradingItem;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;

public class SystemUtil {

	/***
	 * 得到ip
	 * 
	 * @param String
	 * @return
	 */
	static public String getLocalIP() {
		String ip = "";

		Enumeration<NetworkInterface> netInterfaces = null;
		try {
			netInterfaces = NetworkInterface.getNetworkInterfaces();
			while (netInterfaces.hasMoreElements()) {
				NetworkInterface ni = netInterfaces.nextElement();
				Enumeration<InetAddress> ips = ni.getInetAddresses();
				while (ips.hasMoreElements()) {
					ip = ips.nextElement().getHostAddress();
					if (ip.indexOf(".") > 0 && !ip.equals("127.0.0.1"))
						break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return ip;
	}

	/***
	 * 压缩GZip
	 * 
	 * @param data
	 * @return
	 */
	public static String gZip(String input) {
		byte[] bytes = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(bos);
			gzip.write(input.getBytes("utf-8"));
			gzip.finish();
			gzip.close();
			bytes = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return new BASE64Encoder().encodeBuffer(bytes);
	}

	/***
	 * 解压GZip
	 * 
	 * @param data
	 * @return
	 */
	public static String unGZip(String input) {
		byte[] bytes = null;
		String out = "";
		ByteArrayInputStream bis = null;
		GZIPInputStream gzip = null;
		ByteArrayOutputStream baos = null;
		try {
			bis = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(input));
			gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			baos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			bytes = baos.toByteArray();
			baos.flush();
			baos.close();
			baos=null;
			gzip.close();
			gzip=null;
			bis.close();
			bis=null;
			out = new String(bytes, "utf-8");
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			if(null!=baos){
				try {
					baos.close();
					baos = null;
				} catch (IOException e) {
				}
				
			}
			
			if(null != gzip){
				try {
					gzip.close();
					gzip=null;
				} catch (IOException e) {
				}
			}
			
			if(null != bis){
				try {
					bis.close();
					bis=null;
				} catch (IOException e) {
				}
				
			}
			
			
		}
		return out;
	}




	/**
	 * 创建赔率数据xml
	 * 
	 * @param ratioDataInput
	 *//*
	public static String createRatioXml(RatioDataInput ratioDataInput) {
		Document document = DocumentHelper.createDocument(); // 创建文档
		Element ratio = document.addElement("rdi");
		ratio.addAttribute("oc", ratioDataInput.getOnlyCode());
		ratio.addAttribute("wc", ratioDataInput.getWebCode());
		ratio.addAttribute("gc", ratioDataInput.getGameCode());
		ratio.addAttribute("tsc", ratioDataInput.getGameCode());
		ratio.addAttribute("sic", ratioDataInput.getGameCode());
		ratio.addAttribute("dt", String.valueOf(ratioDataInput.getGameCode()));
		ratio.addAttribute("rt", String.valueOf(ratioDataInput.getRatioType()));
		ratio.addAttribute("sn", ratioDataInput.getGameCode() + "");
		ratio.addAttribute("un", ratioDataInput.getUserName());

		List<Sportinfo> sportinfos = ratioDataInput.getSportinfos();
		if (null != sportinfos && sportinfos.size() > 0) {
			for (Sportinfo sportinfo : sportinfos) {
				Element league = ratio.addElement("lgu");
				league.addAttribute("nm", sportinfo.getName());
				league.addAttribute("id", sportinfo.getSelfID());

				List<Matchinfo> matchs = sportinfo.getMatchs();
				for (Matchinfo match : matchs) {
					Element matchElement = league.addElement("mth");
					matchElement.addAttribute("nm", match.getName());
					matchElement.addAttribute("id", match.getSelfID());
					matchElement.addAttribute("snt", match.getSnatchTime());
					matchElement.addAttribute("st", match.getStartTime());

					LiveMatch liveMatch = match.getLiveMatch();
					if (null != liveMatch) {
						Element lm = matchElement.addElement("lm");
						lm.addAttribute("lnm", liveMatch.getLeagueName());
						lm.addAttribute("mnm", liveMatch.getMatchName());
						lm.addAttribute("sht", liveMatch.getShowTime());
						lm.addAttribute("st", liveMatch.getSystemTime());
						lm.addAttribute("sts", String.valueOf(liveMatch.getStatus()));
						lm.addAttribute("csts", String.valueOf(liveMatch.getCloseStatus()));
						lm.addAttribute("hyc", String.valueOf(liveMatch.getHomeYC()));
						lm.addAttribute("ayc", String.valueOf(liveMatch.getAwayYC()));
						lm.addAttribute("hg", String.valueOf(liveMatch.getHomeGoal()));
						lm.addAttribute("ag", String.valueOf(liveMatch.getAwayGoal()));
					}

					List<Playinfo> playinfos = match.getPlayinfos();
					for (Playinfo playinfo : playinfos) {
						Element play = matchElement.addElement("ply");
						play.addAttribute("nm", playinfo.getName());
						play.addAttribute("gt", String.valueOf(playinfo.getGametime()));
						play.addAttribute("id", playinfo.getSelfID());

						List<TradingItem> tradingItems = playinfo.getTradingItems();
						for (TradingItem tradingItem : tradingItems) {
							Element tradeItem = play.addElement("im");
							tradeItem.addAttribute("nm", tradingItem.getName());
							tradeItem.addAttribute("id", tradingItem.getSelfID());
							tradeItem.addAttribute("rt", String.valueOf(tradingItem.getRatio()));
						}
					}
				}
			}
		}
		// 格式化输出格式
		OutputFormat format = OutputFormat.createPrettyPrint();
		// 默认为Utf-8 编码，可以根据需要改变编码格式
		// format.setEncoding("gb2312");
		StringWriter writer = new StringWriter();
		// 格式化输出流
		XMLWriter xmlWriter = new XMLWriter(writer, format);
		// 将document写入到输出流
		try {
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return writer.toString();
	}*/

	/**
	 * 创建盘中信息xml
	 * 
	 * @param matchInfoDataInput
	 *//*
	public static String createMatchInfoXml(MatchInfoDataInput matchInfoDataInput) {
		Document document = DocumentHelper.createDocument(); // 创建文档
		Element matchInfo = document.addElement("mdi");
		matchInfo.addAttribute("oc", matchInfoDataInput.getOnlyCode());
		matchInfo.addAttribute("ts", matchInfoDataInput.getThirdSystem());
		matchInfo.addAttribute("si", matchInfoDataInput.getSnatchItem());
		matchInfo.addAttribute("sn", matchInfoDataInput.getSeqNum() + "");
		matchInfo.addAttribute("un", matchInfoDataInput.getUserName());

		List<LiveMatch> liveMatchs = matchInfoDataInput.getLiveMatchs();
		if (null != liveMatchs && liveMatchs.size() > 0) {
			for (LiveMatch liveMatch : liveMatchs) {
				Element lm = matchInfo.addElement("lm");
				lm.addAttribute("lid", liveMatch.getLeagueId());
				lm.addAttribute("lnm", liveMatch.getLeagueName());
				lm.addAttribute("mid", liveMatch.getMatchId());
				lm.addAttribute("mnm", liveMatch.getMatchName());
				lm.addAttribute("sht", liveMatch.getShowTime());
				lm.addAttribute("st", liveMatch.getSystemTime());
				lm.addAttribute("sts", String.valueOf(liveMatch.getStatus()));
				lm.addAttribute("csts", String.valueOf(liveMatch.getCloseStatus()));
				lm.addAttribute("hyc", String.valueOf(liveMatch.getHomeYC()));
				lm.addAttribute("ayc", String.valueOf(liveMatch.getAwayYC()));
				lm.addAttribute("hg", String.valueOf(liveMatch.getHomeGoal()));
				lm.addAttribute("ag", String.valueOf(liveMatch.getAwayGoal()));
			}
		}
		// 格式化输出格式
		OutputFormat format = OutputFormat.createPrettyPrint();
		// 默认为Utf-8 编码，可以根据需要改变编码格式
		// format.setEncoding("gb2312");
		StringWriter writer = new StringWriter();
		// 格式化输出流
		XMLWriter xmlWriter = new XMLWriter(writer, format);
		// 将document写入到输出流
		try {
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return writer.toString();
	}*/

	public static void main(String[] args) {
		System.out.println(getLocalIP());
	}

}
